<template>
    <div>
        <div class="row">
            <a-button type="primary" :disabled="handle == 'view'" @click="handleBatchUpdateSeckillPrice">批量设置秒杀库存价格</a-button>
        </div>
        <a-table :columns="columns" :dataSource="tableData" :rowKey="record=>record.id">
            <!--是否参与秒杀-->
            <template slot="enableSeckill" slot-scope="text, record,index">
                <a-switch v-model="record.enableSeckill" @change="handleSwitch(record,index)" :disabled="handle == 'view' || record.goodsInventory==0"/>
            </template>
            <!--秒杀库存-->
            <template slot="seckillStock" slot-scope="text, record">
                <div class="input_container">
                    <a-input-number v-model="record.seckillStock" :precision="0" :min="record.orderCount || 1" :max="record.goodsInventory-record.orderCount" @change="handleSeckillStockChange(record)" :disabled="handle == 'view' || record.goodsInventory==0 || record.enableSeckill==false"/>
                    <div class="err_msg" v-if="record.seckillStockErrorMessage">{{record.seckillStockErrorMessage || ''}}</div>
                </div>
            </template>
            <!--秒杀价（元）-->
            <template slot="seckillPrice" slot-scope="text, record">
                <div class="input_container">
                    <a-input-number v-model="record.seckillPrice" :precision="2" :min="0.1" :max="Number(record.salePrice)" @change="handleSeckillPriceChange(record)" :disabled="handle == 'view' || record.goodsInventory==0 || record.enableSeckill==false"/>
                    <div class="err_msg" v-if="record.seckillPriceErrorMessage">{{record.seckillPriceErrorMessage || ''}}</div>
                </div>
            </template>
        </a-table>
        <seckillPrice :visible="seckillPriceVisible" :minGoodsInventory="minGoodsInventory" :minSalePrice="minSalePrice" :maxOrderCount="maxOrderCount" @cancel="seckillPriceVisible=false" @submit="handleSeckillPriceSubmit"></seckillPrice>
    </div>
</template>

<script>
import seckillPrice from './seckillPrice';

export default {
    components:{
        seckillPrice
    },
    props:{
        tableData:{
            type:Array,
            default(){
                return [];
            }
        },
        enableMspage: {
            type: String,
            default: '0'
        },
        handle: {
            type: String,
            default: 'edit'
        }
    },
    data(){
        return {
            columns:[{
                title:'是否参与秒杀',
                dataIndex:'enableSeckill',
                scopedSlots:{ customRender: 'enableSeckill' },
            },{
                title:'商品规格',
                dataIndex:'normName',
            },{
                title:'现有库存',
                dataIndex:'goodsInventory',
                customRender:(text)=>{
                    if(text==0){
                        return <span class="color_red">库存不足</span>;
                    }
                    return text;
                }
            },{
                title:'秒杀库存数量',
                dataIndex:'seckillStock',
                scopedSlots:{ customRender: 'seckillStock' },
            },{
                title:'原价（销售价）',
                dataIndex:'salePrice',
                key: 'salePrice'
            },{
                title:'秒杀价',
                dataIndex:'seckillPrice',
                scopedSlots:{ customRender: 'seckillPrice' },
            },{
                title:'已下单数量',
                dataIndex:'orderCount'
            }],
            seckillPriceVisible:false,
            minGoodsInventory: 1,
            minSalePrice: 0.1,
            maxOrderCount: 0
        }
    },
    created(){
        let result = this.isValidateTableData();
        this.$emit('change',result?this.tableData:false);
    },
    watch: {
        tableData: {
            handler(n, o) {
                console.log(n)
                this.getMinGoodsInventory()
                this.getMinSalePrice()
                this.getMaxOrderCount()
            },
            deep: true,
        }
    },
    methods:{
        // 获取最小库存
        getMinGoodsInventory() {
            let goodsInventorys = this.tableData.map((value, index, array) => {
                return value.goodsInventory
            })
            if(goodsInventorys.length > 0) {
                this.minGoodsInventory = Math.min(...goodsInventorys)
            }
        },
        // 获取最小原价
        getMinSalePrice() {
            let salePrices = this.tableData.map((value, index, array) => {
                return value.salePrice
            })
            if(salePrices.length > 0) {
                this.minSalePrice = Math.min(...salePrices)
            }
        },
        // 获取最大订单数
        getMaxOrderCount() {
            let orderCounts = this.tableData.map((value, index, array) => {
                return value.orderCount
            })
            if(orderCounts.length > 0) {
                this.maxOrderCount = Math.min(...orderCounts)
            }
        },
        //是否参与秒杀
        handleSwitch(item,index){
            //this.$emit('change',this.tableData);
            if(item.enableSeckill){
                let result = this.isValidateTableData();
                this.$emit('change',result?this.tableData:false);
            }
            else {
                item.seckillStockErrorMessage='';
                item.seckillPriceErrorMessage='';
            }
        },
        // 秒杀库存
        handleSeckillStockChange(item){
            let result = this.isValidateTableData();
            this.$emit('change',result?this.tableData:false);
        },
        //秒杀价
        handleSeckillPriceChange(item){
            let result = this.isValidateTableData();
            this.$emit('change',result?this.tableData:false);
        },
        //验证
        isValidateTableData(){
            let isValidate=true;
            this.tableData.forEach(item=>{
                if(item.enableSeckill && item.goodsInventory>0){
                    // 秒杀库存数量
                    if(item.seckillStock == '' || item.seckillStock == null || item.seckillStock == undefined){//为空
                        item.seckillStockErrorMessage='请设置秒杀库存数量';
                        isValidate=false;
                    } else if(item.seckillStock > item.goodsInventory){
                        isValidate=false;
                        item.seckillStockErrorMessage='秒杀库存数量不能大于现有库存数量';
                    } else {//其他情况
                        item.seckillStockErrorMessage='';
                    }
                    //秒杀价
                    console.log('seckillPrice',item.seckillPrice,'salePrice',item.salePrice)
                    if(item.seckillPrice>0 && item.seckillPrice<=item.salePrice){//正常
                        item.seckillPriceErrorMessage='';
                    } else if(item.seckillPrice == '' || item.seckillPrice == null || item.seckillPrice == undefined){//为空
                        item.seckillPriceErrorMessage='请设置秒杀价价格';
                        isValidate=false;
                    } else if(item.seckillPrice>item.salePrice){
                        isValidate=false;
                        item.seckillPriceErrorMessage='秒杀价大于原价';
                    } else {//其他情况
                        item.seckillPriceErrorMessage='';
                    }
                }
            });
            return isValidate;
        },
        //批量计算秒杀价
        batchCalcSeckillPrice(data){
            console.log(data)
            this.tableData.forEach(item=>{
                // 是否开启秒杀
                item.enableSeckill=true;
                // 秒杀库存
                item.seckillStock=data.seckillStock;
                // 秒杀价
                item.seckillPrice=data.seckillPrice;
            });
            let result = this.isValidateTableData();
            this.$emit('change',result?this.tableData:false);
        },
        //批量设置秒杀价
        handleBatchUpdateSeckillPrice(){
            this.seckillPriceVisible=true;
        },
        //批量设置秒杀价回调
        handleSeckillPriceSubmit(data){
            console.log(data);
            this.seckillPriceVisible=false;
            this.batchCalcSeckillPrice(data);
            this.$message.success('批量设置成功，请点击保存按钮！');
        }
    }
}
</script>

<style lang="less" scoped>
.row{
    margin-top:10px;
    margin-bottom:10px;
}
.color_red{
    color:red;
}
.input_container{
    position:relative;
    display: inline;
    .err_msg{
        color:red;
        position: absolute;
        white-space: nowrap;
        left: 50%;
        top: 24px;
        transform: translate(-50%, 0);
        font-size:13px;
    }
}
</style>